package util;

import exception.UnsupportedTypeException;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

public class SimpleBeanConvertor {
    private static Logger logger = Logger.getLogger(SimpleBeanConvertor.class);

    public static <T> T convert(HttpServletRequest request, Class<T> targetType) throws InstantiationException, IllegalAccessException, InvocationTargetException, ParseException, UnsupportedTypeException {
        Map<String, String[]> params = request.getParameterMap();
        System.out.println("数据：" + params);
        if (params.size() != 0) {
            T target = targetType.newInstance();
            Method[] allMethods = targetType.getMethods();
            if (allMethods != null && allMethods.length > 0) {
                for (Method method : allMethods) {
                    if (method.getName().startsWith("set")) {
                        Class[] args = method.getParameterTypes();
                        if (args.length == 1) {
                            String paramName = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
                            if (params.containsKey(paramName)) {
                                try {
                                    Object value = paramValues(params.get(paramName), args[0]);
                                    System.out.println(paramName);
                                    method.invoke(target, value);
                                    System.out.println("---------------------");
                                } catch (InvocationTargetException e) {
                                    logger.debug("参数转换错误", e);
                                    e.printStackTrace();
                                    throw e;
                                }
                            } else if (Boolean.class == args[0] || boolean.class == args[0]) {
                                method.invoke(target, false);
                            }
                        }
                    }
                }
            }
            return target;
        }
        return null;
    }

    public static Object paramValues(String[] value, Class type) throws ParseException, UnsupportedTypeException {
        if (value[0].equals("")) {
            return null;
        }
        if (type.isArray()) {
            return value;
        }
        if (String.class == type) {
            return value[0];
        }
        if (java.util.Date.class == type) {
            System.out.println(value[0]);
            return new SimpleDateFormat("yyyy-MM-dd").parse(value[0]);
        }
        if (boolean.class == type || Boolean.class == type) {
            return true;
        }
        if (short.class == type || Character.class == type) {
            return value[0].charAt(0);
        }
        if (int.class == type || Integer.class == type) {
            return Integer.valueOf(value[0]);
        }
        if (short.class == type || double.class == type || long.class == type || float.class == type || double.class == type) {
            try {
                type = Class.forName("java.lang." + type.getName().substring(0, 1).toLowerCase() + type.getName().substring(1));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        if (type.getName().startsWith("java.lang.") || type.getName().startsWith("java.math.")) {
            try {
                return type.getConstructor(String.class).newInstance(value[0]);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            throw new UnsupportedTypeException();
        }
        return null;
    }
}
